package com.hung.pro.iFlashCards.ui;

import javax.swing.*;
import javax.swing.plaf.ComponentInputMapUIResource;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * Common helper to create UI
 * User: hung
 * Date: 2008/05/04
 * Time: 11:41:09
 * To change this template use File | Settings | File Templates.
 */
public class UIHelper {
    public static AbstractButton createToolBarButton(String iconName, String name, String toolTipText) {
        JButton button = new JButton(readImageIcon(iconName));
        button.setToolTipText(toolTipText);
        button.setName(name);
        button.setFocusable(true);
        return button;
    }

    public static AbstractButton createToolBarButton(String iconName, String toolTipText, ActionListener action, KeyStroke keyStroke) {
        AbstractButton button = createToolBarButton(iconName, "", toolTipText);
        button.registerKeyboardAction(action, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
        return button;
    }

    /**
     * Looks up and returns an icon for the specified filename suffix.
     */
    public static ImageIcon readImageIcon(String filename) {
        URL url = UIHelper.class.getResource("resources/images/" + filename);
        return new ImageIcon(url);
    }

    /**
     * Creates and returns a JToggleButton configured for use in a JToolBar.<p>
     * <p/>
     * This is a simplified method that is overriden by the Looks Demo.
     * The full code uses the JGoodies UI framework's ToolBarButton
     * that better handles platform differences.
     */
    public static AbstractButton createToolBarRadioButton(String iconName, String toolTipText) {
        JToggleButton button = new JToggleButton(readImageIcon(iconName));
        button.setToolTipText(toolTipText);
        button.setFocusable(true);
        return button;
    }

    public static JMenuItem createMenuItem(String text, KeyStroke key, ActionListener action) {
        JMenuItem menuItem = new JMenuItem(text);
        setMenuItemText(menuItem, text);
        menuItem.setAccelerator(key);
        menuItem.addActionListener(action);
        return menuItem;
    }

    public static JMenuItem createMenuItem(String text, ActionListener action) {
        JMenuItem menuItem = new JMenuItem(text);
        setMenuItemText(menuItem, text);
        menuItem.addActionListener(action);
        return menuItem;
    }

    public static JMenuItem createMenuItem(String text) {
        JMenuItem menuItem = new JMenuItem(text);
        setMenuItemText(menuItem, text);
        return menuItem;
    }

    public static JRadioButtonMenuItem createRadioButtonMenuItem(String text, boolean selected, ActionListener action) {
        JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(text, selected);
        setMenuItemText(menuItem, text);
        menuItem.addActionListener(action);
        return menuItem;
    }

    public static void setMenuItemText(AbstractButton menuItem, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        menuItem.setText(result.toString());
        if (haveMnemonic) {
            menuItem.setMnemonic(mnemonic);
            menuItem.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    public static void setKeyAcceleratorForButton(AbstractButton button, KeyStroke keyStroke, AbstractAction action) {
        ActionMap actionMap = button.getActionMap();
        actionMap.put("doClick", action);
        InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(keyStroke, "doClick");
        button.setActionMap(actionMap);


    }

    private static InputMap createInputMap(int condition, AbstractButton button) {
        if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
            return new ComponentInputMapUIResource(button);
        }
        return null;
    }
}
